//
//  MCLoginMobileVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/2.
//

import UIKit
import QMUIKit
import AutoInch
import Toast_Swift
import SwiftyJSON
import SnapKit

class MCLoginMobileVc: MCBaseVc {
    
    
    var datModel:MCData!
    
    var binding:Bool = false
    
        
    var mobileTF:UITextField!
    
    var codeTF:UITextField!
        
    
    override func viewDidLoad() {
        super.viewDidLoad()

       //UI
       loadSubViews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationItem.hidesBackButton = true
        
    }
    
    
    func loadSubViews() -> Void{
        
        
        let backButton = UIButton()
        backButton.setImage(UIImage(named: "nav_back")?.withTintColor(UIColor.white), for: .normal)
        backButton.addTarget(self, action: #selector(clickBack), for: .touchUpInside)
        self.view.addSubview(backButton)
        backButton.snp.makeConstraints { make in
            make.left.equalTo(self.view.snp.left).offset(16.0)
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(-10.0)
            make.width.height.equalTo(30)
        }
        
        
        let loginTitleLabel = UILabel.init()
        loginTitleLabel.text = "登录"
        loginTitleLabel.font = UIFont(name: "PingFangSC-Medium", size: 20)
        loginTitleLabel.textColor = UIColor.white
        view.addSubview(loginTitleLabel)
        loginTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(35.auto())
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(40.auto())
            
        }
        
        //手机号
        let mobileTextField = QMUITextField.init()
        mobileTextField.placeholder =  "输入手机号"
        mobileTextField.keyboardType = .numberPad
        mobileTextField.maximumTextLength = 11
        mobileTextField.textColor = UIColor.white
        view.addSubview(mobileTextField)
        mobileTextField.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(35.auto())
            make.right.equalTo(view.snp.right).offset(-35.auto())
            make.top.equalTo(loginTitleLabel.snp.bottom).offset(10)
            make.height.equalTo(45)
        }
        
        mobileTF = mobileTextField
        
        //手机号下划线
        let lineMobileView = UIView.init()
        lineMobileView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
        view.addSubview(lineMobileView)
        lineMobileView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(35.auto())
            make.right.equalTo(view.snp.right).offset(-35.auto())
            make.top.equalTo(mobileTextField.snp.bottom).offset(1)
            make.height.equalTo(0.5)
        }
        
        
        let codeBtn = WLCaptcheButton.init()
        codeBtn.setTitle("获取验证码", for: .normal)
        codeBtn.setTitleColor(UIColor.qmui_color(withHexString: "#35DE94"), for: .normal)
        codeBtn.titleLabel?.font = UIFont(name: "PingFangSC-Medium", size: 15)
        codeBtn.contentHorizontalAlignment = .right
        codeBtn.addTarget(self, action: #selector(loginSenderAction(sender:)), for: .touchUpInside)
        view.addSubview(codeBtn)
        codeBtn.snp.makeConstraints { make in
            make.right.equalTo(view.snp.right).offset(-35.auto())
            make.top.equalTo(mobileTextField.snp.bottom).offset(25)
            make.height.equalTo(45)
        }
        
        
        //验证码下划线
        let codeTextField = QMUITextField.init()
        codeTextField.placeholder = "请输入验证码"
        codeTextField.keyboardType = .numberPad
        codeTextField.maximumTextLength = 6
        codeTextField.textColor = UIColor.white
        view.addSubview(codeTextField)
        codeTextField.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(35.auto())
            make.right.equalTo(codeBtn.snp.left).offset(-35.auto())
            make.top.equalTo(mobileTextField.snp.bottom).offset(25)
            make.height.equalTo(45)
        }
        codeTF = codeTextField
        
        
        
        //验证码下划线
        let lineCodeView = UIView.init()
        lineCodeView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
        view.addSubview(lineCodeView)
        lineCodeView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(35.auto())
            make.right.equalTo(view.snp.right).offset(-35.auto())
            make.top.equalTo(codeTextField.snp.bottom).offset(1)
            make.height.equalTo(1)
        }
        
        
        //登录或者注册
        let loginBtn = UIButton.init()
        loginBtn.setTitle("登录/注册", for: .normal)
        loginBtn.layer.cornerRadius = 25
        loginBtn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        loginBtn.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
        loginBtn.titleLabel?.font = UIFont(name: "PingFangTC-Semibold", size: 15)
        view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(15)
            make.right.equalTo(view.snp.right).offset(-15)
            make.height.equalTo(50)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-30)
        }
    }
    
    @objc func clickBack() {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    @objc func loginSenderAction(sender:WLCaptcheButton) -> Void {
        
        
        if binding == true {
            
            self.bindingMobileSend(sender: sender)
            
        }else{
            
            if mobileTF.text?.isEmpty == false {
                NetWorkRequest(MCLoginApi.mobileSend(mobile:mobileTF.text!)) {responseModel in
                    if responseModel.code == 200 {
                        self.view.makeToast("发送成功",position: .center)
                    }
                } failureCallback: { ResponseModel in
                    self.view.makeToast("请求失败",position: .center)
                }
            
                WLButtonCountdownManager.default().scheduledCountDown(withKey: "mcbodyBuildingPro", timeInterval: 60) { second in
                    print("\(second)")
                    sender.isEnabled = false
                    sender.setTitle(String(format: "%02ds", Int(second)), for: .normal);
                    if Int(second) == 1 {
                        sender.isEnabled = true
                        sender.setTitle("重新发送", for: .normal)
                    }
                }
            }else{
                self.view.makeToast("手机号不能为空",position: .center)
            }
            
            
        }
        
               
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }
    
    
    ///登录信息
    @objc func loginAction() -> Void {
        
        if mobileTF.text?.isEmpty == true {
            self.view.makeToast("手机号不能为空",position: .center)
            return
        }
        
        if codeTF.text?.isEmpty == true {
            self.view.makeToast("验证码不能为空",position: .center)
            return
        }
        
        self.view.makeToastActivity(.center)
        
        
        if binding == true {
            NetWorkRequest(MCLoginApi.mobileBind(mobile: mobileTF.text, code: codeTF.text)) { reponseModel in
                self.view.hideToastActivity()
                
               let dataModel = MCData.init(JSONString: reponseModel.dataString)
                
                UserDefaults.standard.set(dataModel?.tokenValue, forKey: "LoginToken")
                UserDefaults.standard.set(dataModel?.tokenName, forKey: "LoginName")
                let appelete:AppDelegate = UIApplication.shared.delegate as! AppDelegate
                appelete.tabbarInfo()
            } failureCallback: { responseMdoel in
                self.view.hideToastActivity()
                self.view.makeToast("登录失败")
            }
        }else{
            NetWorkRequest(MCLoginApi.mobileApi(mobile: mobileTF.text, verif: codeTF.text)) { reponseModel in
                self.view.hideToastActivity()
                
                
               let dataModel = MCData.init(JSONString: reponseModel.dataString)
                
                UserDefaults.standard.set(dataModel?.tokenValue, forKey: "LoginToken")
                UserDefaults.standard.set(dataModel?.tokenName, forKey: "LoginName")
                let appelete:AppDelegate = UIApplication.shared.delegate as! AppDelegate
                appelete.tabbarInfo()
            } failureCallback: { responseMdoel in
                self.view.hideToastActivity()
                self.view.makeToast("登录失败")
            }
        }
        
        
    }

    
    ///
    func bindingMobileSend(sender:WLCaptcheButton) -> Void {
        
        if mobileTF.text?.isEmpty == false {
            NetWorkRequest(MCLoginApi.mobileBindSend(mobile:mobileTF.text!)) {responseModel in
                if responseModel.code == 200 {
                    self.view.makeToast("发送成功",position: .center)
                }
            } failureCallback: { ResponseModel in
                self.view.makeToast("请求失败",position: .center)
            }
        
            WLButtonCountdownManager.default().scheduledCountDown(withKey: "mcbodyBuildingPro", timeInterval: 60) { second in
                print("\(second)")
                sender.isEnabled = false
                sender.setTitle(String(format: "%02ds", Int(second)), for: .normal);
                if Int(second) == 1 {
                    sender.isEnabled = true
                    sender.setTitle("重新发送", for: .normal)
                }
            }
        }else{
            self.view.makeToast("手机号不能为空",position: .center)
        }
        
       
    }
    
    
}
